package zjc.bookkeeping.tools;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

import zjc.bookkeeping.R;
import zjc.bookkeeping.db.DBManager;

public class BusinessDialog extends Dialog {
    String business;
    SearchView searchView;
    ListView listView;
    ArrayAdapter adapter;
    List<String> data = DBManager.getAllBusiness();
    public interface OnBusinessItemListener{
        public void onBusinessItemClick();
    }
    OnBusinessItemListener onBusinessItemListener;

    public void setOnBusinessItemListener(OnBusinessItemListener onBusinessItemListener) {
        this.onBusinessItemListener = onBusinessItemListener;
    }

    public BusinessDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_business);
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        initView();
        searchView.setFocusable(false);
        searchView.clearFocus();
        adapter = new ArrayAdapter<String>(getContext(), R.layout.dialog_business_item,data);
        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);
        businessListener();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        getWindow().setAttributes(layoutParams);
    }

    private void businessListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView textView= view.findViewById(R.id.dialog_business_item_tv);
                String str = textView.getText().toString();
                if (str.equals("创建“"+business+"”")){
                    DBManager.createBusiness(business);
                }else {
                    business = str;
                }
                onBusinessItemListener.onBusinessItemClick();
                cancel();
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if (!TextUtils.isEmpty(s)) {
                    List<String> list = new ArrayList<String>();
                    int size = data.size();
                    for (int i=0; i<size; i++){
                        if (data.get(i).contains(s)){
                            list.add(data.get(i));
                        }
                    }
                    if (list.size() == 0){
                        list.add("创建“"+s+"”");
                    }
                    adapter = new ArrayAdapter<String>(getContext(), R.layout.dialog_business_item,list);
                    listView.setAdapter(adapter);
                }else {
                    adapter = new ArrayAdapter<String>(getContext(), R.layout.dialog_business_item,data);
                    listView.setAdapter(adapter);
                }
                business = s;
                return false;
            }
        });
    }

    private void initView() {
        searchView = findViewById(R.id.dialog_business_sv);
        listView = findViewById(R.id.dialog_business_lv);
    }
//    获取商家
    public String getBusiness() {
        return business;
    }
//    设置Dialog的控件尺寸
    public void setBusinessDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);
    }
}
